<template>
  <div>
    <div class="header">
      <van-icon name="arrow-left" class="back-icon" @click="$router.go(-1)" />
      <span class="header-text">确认订单</span>
    </div>
    <div class="pay-content">
      <div class="card1">
        <div class="info">
          <div class="address">{{ addressDef.address }}</div>
          <div class="userInfo">
            <div class="nick">{{ addressDef.nick }}</div>
            <div class="phone">{{ addressDef.phone }}</div>
          </div>
        </div>
        <div class="card1-icon">
          <van-icon name="arrow" @click="goAddress" />
        </div>
      </div>
      <div class="card2">
        <div class="code">
          <span>订单号：</span>
          <span>{{ order_code }}</span>
        </div>
        <ul class="order-list">
          <li v-for="(item, index) in orderList" :key="index">
            <div class="goods-pic">
              <img :src="item.goods_img" />
            </div>
            <div class="goods-info">
              <div class="goods-nane">{{ item.goods_name }}</div>
              <div class="goods-choose">{{ item.goods_choose }}</div>
            </div>
            <div class="goods-num">
              <div class="price">
                <span>¥</span>{{ item.goods_price * item.goods_num }}
              </div>
              <div class="num">x{{ item.goods_num }}</div>
            </div>
          </li>
        </ul>
        <van-cell-group>
          <van-field
            v-model="discount"
            label="优惠券"
            placeholder="暂无可用"
            readonly
            right-icon="arrow"
            input-align="right"
          />
        </van-cell-group>
        <van-cell-group>
          <van-field
            v-model="remarks"
            label="订单备注"
            placeholder="点击填写备注"
            right-icon="arrow"
            input-align="right"
          />
        </van-cell-group>
        <div class="order-info">
          共计 {{ allNum }} 件商品，合计 <span><i>¥</i>{{ total }}</span>
        </div>
      </div>
      <div class="card3">
        <div class="pay-way">支付方式</div>
        <div class="wx-pay">
          <van-icon name="alipay" class="zfb-icon" />支付宝支付
          <van-checkbox
            icon-size="18px"
            class="radio"
            v-model="checked"
          ></van-checkbox>
        </div>
      </div>
      <div class="bottom">
        <div class="bottom-left">
          <span class="bottom-text">待支付</span>
          <span class="money-icon">¥</span>
          <span class="num">{{ total }}</span>
        </div>
        <van-button
          class="bottom-right"
          :disabled="!checked"
          color="#143ea9"
          @click="PaySuccess"
          >支付</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import {
  reqgetOrder,
  reqDelAllShopCart,
  reqGetUserInfo,
  reqZfbPay,
} from "@/api";
import { Toast } from "vant";
export default {
  data() {
    return {
      checked: true,
      discount: "",
      remarks: "",
      orderList: [],
      total: 0,
      order_code: "",
      addressDef: {},
      addressList: [],
    };
  },
  computed: {
    allNum() {
      let num = 0;
      this.orderList.forEach((item) => {
        num += Number(item.goods_num);
      });
      return num;
    },
  },
  mounted() {
    this.getOrder();
    this.getAdd();
  },
  methods: {
    goAddress() {
      this.$router.push({
        name: "address",
        params: {
          backWhere: "Pay",
        },
      });
    },
    async getAdd() {
      let id = this.$store.state.login.userInfo.id;
      let res = await reqGetUserInfo({ id });
      if (res.status === 200) {
        this.addressList = JSON.parse(res.data.address);
      }
    },
    async getOrder() {
      let user_id = this.$store.state.login.userInfo.id;
      let res = await reqgetOrder({ user_id });
      if (res.status === 200) {
        this.orderList = JSON.parse(res.data.goods_info);
        this.total = res.data.total;
        this.order_code = res.data.order_code;
      }
    },
    async delAllGoods() {
      let user_id = this.$store.state.login.userInfo.id;
      let res = await reqDelAllShopCart({ user_id });
      if (res.status === 200) {
        this.orderShow = false;
        this.cartShow = false;
      }
    },
    async PaySuccess() {
      /**
       *  let orderId = req.body.orderId;
          let orderMoney = req.body.orderMoney;
          let orderTitle = req.body.orderTitle;
          let orderInfo = req.body.orderInfo;
       */
      let orderTitle = [];
      let orderInfo = [];
      this.orderList.forEach((item) => {
        orderTitle.push(item.goods_name);
        orderInfo.push(item.goods_choose);
      });

      let orderObj = {
        orderId: this.order_code,
        orderMoney: this.total,
        orderTitle: orderTitle.toString(),
        orderInfo: orderInfo.toString(),
      };
      let res = await reqZfbPay(orderObj);
      if (res.code === 200) {
        // 支付成功后，需要将购物车中的数据进行删除
        this.delAllGoods();
        Toast("正在支付，请稍等～");
        window.location.href = res.result;
      }
    },
  },
  watch: {
    addressList(val) {
      this.addressList.forEach((item) => {
        if (item.default) {
          this.addressDef = item;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 50px;
  background-color: #fff;
  padding: 0 10px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
  .back-icon {
    position: absolute;
    left: 10px;
    font-size: 18px;
  }
  .header-text {
    color: #323233;
    font-weight: 500;
  }
}
.pay-content {
  padding: 10px 10px 150px;
  box-sizing: border-box;
  .card1 {
    width: 100%;
    height: 80px;
    background-color: #fff;
    border-radius: 15px;
    margin-bottom: 10px;
    box-shadow: 0 0 3px rgb(0 0 0 / 10%);
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      height: 100%;
      .address {
        font-weight: 400;
        font-size: 15px;
      }
      .userInfo {
        display: flex;
        font-size: 14px;
        color: #888888;
        .nick {
          margin-right: 15px;
        }
      }
    }
  }
  .card2 {
    width: 100%;
    // min-height: 220px;
    // max-height: 290px;
    background-color: #fff;
    border-radius: 15px;
    margin-bottom: 10px;
    box-shadow: 0 0 3px rgb(0 0 0 / 10%);
    padding: 15px 10px;
    box-sizing: border-box;
    .code {
      margin-bottom: 20px;
      font-size: 13px;
      color: #969799;
    }
    .order-list {
      // max-height: 140px;
      li {
        width: 100%;
        height: 70px;
        background-color: #fff;
        display: flex;
        align-items: center;
        .goods-pic {
          height: 60px;
          width: 60px;
          text-align: center;
          img {
            height: 100%;
          }
        }
        .goods-info {
          align-self: flex-start;
          margin-left: 10px;
          .goods-nane {
            margin-bottom: 10px;
            font-size: 15px;
          }
          .goods-choose {
            font-size: 13px;
            color: #969799;
          }
        }
        .goods-num {
          align-self: flex-start;
          margin-left: auto;
          justify-content: flex-end;
          .price {
            margin-bottom: 10px;
            font-size: 15px;
            span {
              font-size: 13px;
            }
          }
          .num {
            font-size: 13px;
            color: #969799;
            display: flex;
            justify-content: end;
          }
        }
      }
    }
    .order-info {
      height: 30px;
      display: flex;
      justify-content: flex-end;
      align-items: flex-end;
      font-size: 13px;
      color: #969799;
      span {
        font-size: 16px;
        color: #333;
        margin-left: 5px;
        i {
          font-size: 13px;
        }
      }
    }
  }
  .card3 {
    width: 100%;
    height: 85px;
    background-color: #fff;
    border-radius: 15px;
    box-shadow: 0 0 3px rgb(0 0 0 / 10%);
    padding: 0 10px;
    box-sizing: border-box;
    color: #646566;
    font-size: 15px;
    .pay-way {
      height: 50%;
      border-bottom: 1px solid #f8f8f8;
      display: flex;
      align-items: center;
    }
    .wx-pay {
      height: 50%;
      display: flex;
      align-items: center;
      .zfb-icon {
        color: #027AFF;
        font-size: 20px;
        margin-right: 5px;
      }
      .radio {
        margin-left: auto;
      }
    }
  }
  .bottom {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    padding: 15px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 6px 2px rgb(0 0 0 / 20%);
    .bottom-left {
      display: flex;
      align-items: baseline;
      .bottom-text {
        font-size: 14px;
        color: #646566;
        margin-right: 5px;
      }
      .money-icon {
        font-weight: 600;
      }
      .num {
        margin-left: 3px;
        font-size: 20px;
        font-weight: 600;
      }
    }
    .bottom-right {
      width: 110px;
      height: 45px;
      background-color: #143ea9;
      color: #fff;
      font-size: 15px;
      border-radius: 25px;
      display: flex;
      align-items: center;
      justify-content: center;
      border: none;
    }
  }
}
::v-deep.van-cell {
  padding: 10px 0;
}
</style>

<style>
.van-checkbox__icon--checked .van-icon {
  background-color: #143ea9;
  border-color: #143ea9;
}
</style>
